package com.horse.cloud.boss.feignclients;

import com.horse.cloud.data.domain.gateway.BusinessSystemBo;
import com.horse.cloud.data.domain.gateway.BusinessSystemQueryBo;
import com.horse.cloud.data.domain.gateway.GatewayServerQueryBo;
import com.horse.cloud.framework.general.enums.ResponseCodeEnums;
import com.horse.cloud.framework.general.response.Response;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @create : Created by intelliJ IDEA 17.2
 * @author : bing.Pan
 * @e-mail : 15923508369@163.com
 * @gmdate : 10 26 2017 17:19
 * @sidesc : feign 客户端集成hystrix 熔断退回实现
 *         这里通过FallbackFactory 实现检查退回原因
 *         MemberAccountClient 的fallback类实现FallbackFactory接口并覆写create 方法
 *         覆写MemberAccountClient接口下的方法，定义接口退回数据和日志内容
 */

@Component
public class GatewayFallBackFactory implements FallbackFactory<GatewayClient>{
    private static final Logger LOGGER = LoggerFactory.getLogger(GatewayFallBackFactory.class);

    @Override
    public GatewayClient create(Throwable throwable) {
        return new GatewayClient() {
            @Override
            public Response findBussinessSystemById(String id) {
                GatewayFallBackFactory.LOGGER.info("fallback; reason was:", throwable);
                return new Response<>(ResponseCodeEnums.MINOR_SERVER_HYSTRIX_FALLBACK,"/gateway/findBussinessSystemById");
            }

            @Override
            public Response addAndUpdate(BusinessSystemBo businessSystemBo) {
                GatewayFallBackFactory.LOGGER.info("fallback; reason was:", throwable);
                return new Response<>(ResponseCodeEnums.MINOR_SERVER_HYSTRIX_FALLBACK,"/gateway/addAndUpdate");
            }

            @Override
            public Response deleteBusinessSystem(String id) {
                GatewayFallBackFactory.LOGGER.info("fallback; reason was:", throwable);
                return new Response<>(ResponseCodeEnums.MINOR_SERVER_HYSTRIX_FALLBACK,"/gateway/deleteBusinessSystem");
            }

            @Override
            public Response businessSystemList(BusinessSystemQueryBo businessSystemQueryBo) {
                GatewayFallBackFactory.LOGGER.info("fallback; reason was:", throwable);
                return new Response<>(ResponseCodeEnums.MINOR_SERVER_HYSTRIX_FALLBACK,"/gateway/businessSystemList");
            }

            @Override
            public Response serverList(GatewayServerQueryBo gatewayServerQueryBo) {
                GatewayFallBackFactory.LOGGER.info("fallback; reason was:", throwable);
                return new Response<>(ResponseCodeEnums.MINOR_SERVER_HYSTRIX_FALLBACK,"/gateway/serverList");
            }

        };
    }
}
